local Lang = require("common.Lang")
local Msg = require("core.Msg")
local YellowExcel = require("excel.Yellow")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local PfLogic = require("tencentApi.PfLogic")
local Buffer   = require("buffer.Buffer")

REWARD_NOT_CAN_GET = 0          --未领取
REWARD_ALREADY_GET = 1          --已领取

TYPE_NEW = 1	-- 新手
TYPE_LV = 2		-- 升级
TYPE_DAILY = 3	-- 每日
TYPE_WEEK = 4 --每周


--领取奖励
function getReward(human, type, index)
	if type ~= TYPE_NEW and type ~= TYPE_LV and type ~= TYPE_DAILY and type ~= TYPE_WEEK then
		return
	end

	if type == 1 then
		if human.db.tgpNew == REWARD_ALREADY_GET then
			return
		end
		if not BagLogic.checkCanAddItemList(human, YellowExcel.tgpNew[1].reward, #YellowExcel.tgpNew[1].reward) then
			return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YELLOW_BAG_FULL)
		end
		human.db.tgpNew = REWARD_ALREADY_GET
		for i=1, #YellowExcel.tgpNew[1].reward do
			BagLogic.addItem(human, YellowExcel.tgpNew[1].reward[i][1], 1, YellowExcel.tgpNew[1].reward[i][2], "tgp")
		end    
	elseif type == 2 then
		if human.db.tgpLv[index - 1] == REWARD_ALREADY_GET then
			return
		end
		if human.db.lv < YellowExcel.tgpLv[index].lv then
			return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YELLOW_HUMAN_LV_NOT_ENOUGH)
		end
		if not BagLogic.checkCanAddItemList(human, YellowExcel.tgpLv[index].reward, #YellowExcel.tgpLv[index].reward) then
			return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YELLOW_BAG_FULL)
		end
		human.db.tgpLv[index - 1] = REWARD_ALREADY_GET
		for i=1, #YellowExcel.tgpLv[index].reward do
			BagLogic.addItem(human, YellowExcel.tgpLv[index].reward[i][1], 1, YellowExcel.tgpLv[index].reward[i][2], "tgp")
		end    
	elseif type == 3 then
		if human.db.tgpDaily == REWARD_ALREADY_GET then
			return
		end
		if not BagLogic.checkCanAddItemList(human, YellowExcel.tgpDaily[1].reward, #YellowExcel.tgpDaily[1].reward) then
			return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YELLOW_BAG_FULL)
		end
		human.db.tgpDaily = REWARD_ALREADY_GET
		for i=1, #YellowExcel.tgpDaily[1].reward do
			BagLogic.addItem(human, YellowExcel.tgpDaily[1].reward[i][1], 1, YellowExcel.tgpDaily[1].reward[i][2], "tgp")
		end  
  elseif type == 4 then  
    if human.db.tgpWeek == REWARD_ALREADY_GET then
      return
    end
    if not BagLogic.checkCanAddItemList(human, YellowExcel.tgpWeek[1].reward, #YellowExcel.tgpWeek[1].reward) then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.YELLOW_BAG_FULL)
    end
    human.db.tgpWeek = REWARD_ALREADY_GET
    for i=1, #YellowExcel.tgpWeek[1].reward do
      BagLogic.addItem(human, YellowExcel.tgpWeek[1].reward[i][1], 1, YellowExcel.tgpWeek[1].reward[i][2], "tgp")
    end  
	end
	sendRewardStatus(human, type)
end

--发送奖励列表
function sendRewardList(human, type)
	if type ~= TYPE_NEW and type ~= TYPE_LV and type ~= TYPE_DAILY and type ~= TYPE_WEEK then
		return
	end

	local sendMsg = Msg.gc.GC_TGP_REWARD_LIST
	sendMsg.type = type
	if type == TYPE_NEW then -- 新手礼包
		sendMsg.rewardList[0] = 1
		sendMsg.rewardList[1].condition = 0
		sendMsg.rewardList[1].reward[0] = #YellowExcel.tgpNew[1].reward
		for i=1, sendMsg.rewardList[1].reward[0] do
			local cache = YellowExcel.tgpNew[1].grids[i]
			Grid.makeItem(cache, sendMsg.rewardList[1].reward[i], 0, human)
		end
	elseif type == TYPE_LV then -- 等级礼包
		sendMsg.rewardList[0] = #YellowExcel.tgpLv
		for i=1,sendMsg.rewardList[0] do
			sendMsg.rewardList[i].condition = YellowExcel.tgpLv[i].lv
			sendMsg.rewardList[i].reward[0] = #YellowExcel.tgpLv[i].reward
			for j=1, sendMsg.rewardList[i].reward[0] do
				local cache = YellowExcel.tgpLv[i].grids[j]
				Grid.makeItem(cache, sendMsg.rewardList[i].reward[j], 0, human)
			end 
		end 
	elseif type == TYPE_DAILY then -- 每日登录礼包
		sendMsg.rewardList[0] = 1
		sendMsg.rewardList[1].condition = 0
		sendMsg.rewardList[1].reward[0] = #YellowExcel.tgpDaily[1].reward
		for i=1, sendMsg.rewardList[1].reward[0] do
			local cache = YellowExcel.tgpDaily[1].grids[i]
			Grid.makeItem(cache, sendMsg.rewardList[1].reward[i], 0, human)
		end
  elseif type == TYPE_WEEK then
    sendMsg.rewardList[0] = 1
    sendMsg.rewardList[1].condition = 0
    sendMsg.rewardList[1].reward[0] = #YellowExcel.tgpWeek[1].reward
    for i=1, sendMsg.rewardList[1].reward[0] do
      local cache = YellowExcel.tgpWeek[1].grids[i]
      Grid.makeItem(cache, sendMsg.rewardList[1].reward[i], 0, human)
    end
	end
	Msg.send(sendMsg, human.fd)
	sendRewardStatus(human, type)
end

--发送状态
function sendRewardStatus(human, type)
	if type ~= TYPE_NEW and type ~= TYPE_LV and type ~= TYPE_DAILY and type ~= TYPE_WEEK then
		return
	end

	local sendMsg = Msg.gc.GC_TGP_REWARD_STATUS
	sendMsg.type = type
	if type == TYPE_NEW then
		sendMsg.status[0] = 1
		sendMsg.status[1] = human.db.tgpNew
	elseif type == TYPE_LV then
		sendMsg.status[0] = #YellowExcel.tgpLv
		for i=1, sendMsg.status[0] do
			sendMsg.status[i] = human.db.tgpLv[i-1]
		end    
	elseif type == TYPE_DAILY then
		sendMsg.status[0] = 1
		sendMsg.status[1] = human.db.tgpDaily
  elseif type == TYPE_WEEK then
    sendMsg.status[0] = 1
    sendMsg.status[1] = human.db.tgpWeek
	end
	Msg.send(sendMsg, human.fd)

end

function clearDailyStatus(human)
	human.db.tgpDaily = 0
	sendRewardStatus(human, TYPE_DAILY)
end

function canShow(human)
	if human.db.isMiniClient == 14 or PfLogic.isWebsite(human) then
		return true
	end
end

function canActShow(human)
  if human.db.isMiniClient ~= 14 and PfLogic.isWebsite(human) ~= true then
    return false
  end
  local tb = {}
  tb.year = YellowExcel.tgpOther[1].startTime[1]
  tb.month = YellowExcel.tgpOther[1].startTime[2]
  tb.day = YellowExcel.tgpOther[1].startTime[3]
  tb.hour = 0
  tb.min = 0
  tb.sec = 0
  local starTime = os.time(tb)
  tb.year = YellowExcel.tgpOther[1].endTime[1]
  tb.month = YellowExcel.tgpOther[1].endTime[2]
  tb.day = YellowExcel.tgpOther[1].endTime[3]
  tb.hour = 23
  tb.min = 59
  tb.sec = 59
  local endTime = os.time(tb)
  if os.time() > starTime and os.time() < endTime then
    return true
  end
end

function init()
	local i
	local j
	
	YellowExcel.tgpNew[1].grids = {}
	for i = 1, #YellowExcel.tgpNew[1].reward do
		YellowExcel.tgpNew[1].grids[i] = Grid.create(nil, YellowExcel.tgpNew[1].reward[i][1], YellowExcel.tgpNew[1].reward[i][2])
	end

	YellowExcel.tgpDaily[1].grids = {}
	for i = 1, #YellowExcel.tgpDaily[1].reward do
		YellowExcel.tgpDaily[1].grids[i] = Grid.create(nil, YellowExcel.tgpDaily[1].reward[i][1], YellowExcel.tgpDaily[1].reward[i][2])
	end
	
	for i = 1, #YellowExcel.tgpLv do
		YellowExcel.tgpLv[i].grids = {}
		for j = 1, #YellowExcel.tgpLv[i].reward do
			YellowExcel.tgpLv[i].grids[j] = Grid.create(nil, YellowExcel.tgpLv[i].reward[j][1], YellowExcel.tgpLv[i].reward[j][2])
		end
	end
  YellowExcel.tgpWeek[1].grids = {}
  for i = 1, #YellowExcel.tgpWeek[1].reward do
    YellowExcel.tgpWeek[1].grids[i] = Grid.create(nil, YellowExcel.tgpWeek[1].reward[i][1], YellowExcel.tgpWeek[1].reward[i][2])
  end
end
--添加buf
function addBuffer(human)
  if human.db.isMiniClient == 14 or PfLogic.isWebsite(human) then
    for i=1, #YellowExcel.tgpOther[1].buf do
      Buffer.add(YellowExcel.tgpOther[1].buf[i], human, human)
    end
  end
end